package com.iexbuy.ihg.wxapi;


import com.iexbuy.ihg.App;
import com.iexbuy.ihg.R;
import com.iexbuy.ihg.common.CommUtil;
import com.iexbuy.ihg.entity.XiaoFeiOrderConfirmObject;
import com.iexbuy.ihg.pay.PayUtil;
import com.iexbuy.ihg.ui.fragment.OrderDetailFrament;
import com.iexbuy.ihg.utils.ToastUtils;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler{
	
	private static final String TAG = "WXPayEntryActivity";
	
    private IWXAPI api;

	private static PayUtil.OnPayCompleteListener onPayCompleteListener;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        
    	api = WXAPIFactory.createWXAPI(this, WeixinPay.APP_ID);

        api.handleIntent(getIntent(), this);

		findViewById(R.id.backBtn).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				WXPayEntryActivity.this.finish();
			}
		});
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
		Log.d(TAG, "startPage, openId = " + req.openId);
	}

	@Override
	public void onResp(BaseResp resp) {
		Log.d(TAG, "onPayFinish, errCode = " + resp.errCode + ", resp.errStr=" + resp.errStr);
		if (resp.errCode == 0) {
			if (onPayCompleteListener != null) {
				onPayCompleteListener.onPayComplete(String.valueOf(resp.errCode));
				onPayCompleteListener = null;
			}
			findViewById(R.id.tips).setVisibility(View.GONE);
			this.finish();
		} else {
			((TextView)findViewById(R.id.payResult)).setText("微信支付失败，请稍后重试，结果代码：" + resp.errCode + ", " + resp.errStr + "。");
			findViewById(R.id.tips).setVisibility(View.VISIBLE);
		}
	}

	public static void setOnPayCompleteListener(PayUtil.OnPayCompleteListener onPayCompleteListener) {
		WXPayEntryActivity.onPayCompleteListener = onPayCompleteListener;
	}
}